<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 区域管理类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang
 * @version 1.0
 */

class sysItemMultilevel{

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 获取基础设置信息
     * @param $arg_array
     * @return array|null
     */
    public function getItemMultilevelList(&$arg_array){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //选项id
        if (isset($arg_array['item_id']) && is_numeric($arg_array['item_id'])) {
            $sql .= ' AND item_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['item_id'];
        }

        //选项名称
        if (isset($arg_array['item_name']) && !empty($arg_array['item_name'])) {
            $sql .= ' AND item_name LIKE ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['item_name'].'%';
        }

        //上级ID
        if (isset($arg_array['parent_id']) && is_numeric($arg_array['parent_id'])) {
            $sql .= ' AND parent_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['parent_id'];
        }

        //分类级别
        if (isset($arg_array['level']) && !empty($arg_array['level'])) {
            $sql .= ' AND level = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['level'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4) ;
        }



        try {

            if (isset($arg_array['rp'])) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_item_multilevel  ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }


            //查询备注信息
            $search_sql = 'SELECT * FROM sys_item_multilevel ' . $sql;

            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array)
                return null;

            if ($temp_item_array) {
                foreach ($temp_item_array as &$item) {

                    $search_son = $this->dbca->stmt_query('SELECT count(*) as num FROM sys_item_multilevel WHERE parent_id = ?','i',$item->item_id);

                    if($search_son[0]->num > 0){
                        $hasMid = 1;
                    }else{
                        $hasMid = 0;
                    }

                    $item_array[] = array(
                        'uuid' => $item->uuid,   //UUID
                        'timeStamp' => $item->timeStamp,   //时间戳
                        'creator' => $item->creator,     //创建人
                        'createTime' => $item->createTime,                //创建时间
                        'item_id' => $item->item_id,                //选项ID
                        'item_name' => $item->item_name,            //选项名称
                        'parent_id' => $item->parent_id,            //上级ID
                        'sort_order' => $item->sort_order,            //排序序号
                        'level' => $item->level,      //分类级别
                        'hasMid' => $hasMid
                    );
                }
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }


    }

    /**
     * 获取信息
     * @param $arg_array
     * @return array|null
     */
    public function searchMidRegion(&$arg_array){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //分类ID
        if (isset($arg_array['item_id']) && is_numeric($arg_array['item_id'])) {
            $sql .= ' AND item_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['item_id'];
        }

        //上级区域ID
        if (isset($arg_array['parent_id'])  && is_numeric($arg_array['parent_id'])) {
            $sql .= ' AND parent_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['parent_id'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_item_multilevel ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_item_multilevel ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $smallNum = $this->dbca->stmt_query('select count(*) as num from sys_item_multilevel where parent_id = ? ','i',$item->parent_id);

                $hasSma = $smallNum[0]->num?1:0;

                $item_array[] = array(
                    'uuid' => $item->uuid,   //UUID
                    'timeStamp' => $item->timeStamp,   //时间戳
                    'creator' => $item->creator,     //创建人
                    'createTime' => $item->createTime,                //创建时间
                    'item_id' => $item->item_id,                //选项ID
                    'item_name' => $item->item_name,            //选项名称
                    'parent_id' => $item->parent_id,            //上级ID
                    'sort_order' => $item->sort_order,            //排序序号
                    'level' => $item->level,      //分类级别
                    'hasSma' => $hasSma
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 保存 编辑第一级信息
     * @param $arg_array
     * @return array|null
     */
    public function saveFirstLevel(&$arg_array){

        if(!isset($arg_array['item_name'], $arg_array['sort_order']) ){
            return array('code'=>400,'message'=>'参数错误');
        }

        try{

            $sys_item_multilevel = new entity('sys_item_multilevel');

            if(isset($arg_array['aid']) && !empty($arg_array['aid'])){
                $sys_item_multilevel->item_id = $arg_array['editStr'];
                if(!$this->dbca->fetch($sys_item_multilevel)){
                    return array('code'=>400,'message'=>'该选项不存在');
                }

                $sql = 'SELECT COUNT(*) AS num,item_id FROM sys_item_multilevel WHERE level = 1 AND item_name = "'.$arg_array['item_name'] .'"';
                $count_arr = &$this->dbca->stmt_query($sql);
                if( $count_arr[0]->num  > 0 && $count_arr[0]->item_id !=$arg_array['editStr'] ){
                    return array('code'=>400,'message'=>'该选项已存在，请重新编辑');
                }

                $sys_item_multilevel->item_name = $arg_array['item_name'];
                $sys_item_multilevel->parent_id = 0;
                $sys_item_multilevel->sort_order = $arg_array['sort_order'];
                $sys_item_multilevel->level = 1;
                $this->dbca->save($sys_item_multilevel);

                $insert_id = $this->obtainSortNum('sys_item_multilevel');

                return array('code'=>200,'message'=>'保存成功','insert_id'=>$insert_id);
            }else{
                $sys_item_multilevel = new entity('sys_item_multilevel');
                $sys_item_multilevel->item_name = $arg_array['item_name'];
                $sys_item_multilevel->level = 1;

                if($this->dbca->fetch($sys_item_multilevel)){
                    return array('code'=>400,'message'=>'该选项已存在，不能重复添加！');
                }
                $sys_item_multilevel->item_id = generate_seqno('seqno_item_multilevel');
                $sys_item_multilevel->creator = $arg_array['creator'];
                //$sys_item_multilevel->item_name = $arg_array['item_name'];
                $sys_item_multilevel->parent_id = 0;
                $sys_item_multilevel->sort_order = $arg_array['sort_order'];
                //$sys_item_multilevel->level = 1;
                $this->dbca->save($sys_item_multilevel);

                $return_array['item_id'] = $sys_item_multilevel->item_id;
                $return_array['item_name'] = $sys_item_multilevel->item_name;
                $return_array['sort_order'] = $sys_item_multilevel->sort_order;
                $return_array['parent_id'] = $sys_item_multilevel->parent_id;

//                $sys_item_multilevel->sortOrder = $arg_array['sort_order'];
//                $insert_id = $this->obtainSortNum('sys_item_multilevel');

                return array('code'=>200,'message'=>'保存成功', 'data'=>$return_array);
//                return array('code'=>200,'message'=>'保存成功','insert_id'=>$insert_id);
            }



        }catch (Exception $e){

            error_log($e->getMessage());
            return null;

        }


    }

    /**
     * 保存 编辑第二级信息
     * @param $arg_array
     * @return array|null
     */
    public function saveSecondLevel(&$arg_array){

        if(!isset( $arg_array['sort_order'], $arg_array['item_name'])){
            return array('code'=>400,'message'=>'参数错误');
        }

        try{

            $sys_item_multilevel = new entity('sys_item_multilevel');

            if(isset($arg_array['aid']) && !empty($arg_array['aid'])){
                $sys_item_multilevel->item_id = $arg_array['editStr'];
                if(!$this->dbca->fetch($sys_item_multilevel)){
                    return array('code'=>400,'message'=>'该选项不存在');
                }

                $sql = 'SELECT COUNT(*) AS num,item_id FROM sys_item_multilevel WHERE level = 2 AND item_name = "'.$arg_array['item_name'] .'" AND parent_id='.$sys_item_multilevel->parent_id;
                $count_arr = &$this->dbca->stmt_query($sql);
                if( $count_arr[0]->num  > 0 && $count_arr[0]->item_id !=$arg_array['editStr']){
                    return array('code'=>400,'message'=>'该选项已存在，请重新编辑');
                }

                $sys_item_multilevel->item_name = $arg_array['item_name'];
//                $sys_item_multilevel->parent_id = $arg_array['pid'];
                $sys_item_multilevel->sort_order = $arg_array['sort_order'];
                $sys_item_multilevel->level = 2;
                $this->dbca->save($sys_item_multilevel);

                $insert_id = $this->obtainSortNum('sys_item_multilevel');

                return array('code'=>200,'message'=>'保存成功','insert_id'=>$insert_id);
            }else{

                $sys_item_multilevel = new entity('sys_item_multilevel');
                $sys_item_multilevel->item_name = $arg_array['item_name'];
                $sys_item_multilevel->level = 2;
                $sys_item_multilevel->parent_id = $arg_array['editStr'];
                if($this->dbca->fetch($sys_item_multilevel)){
                    return array('code'=>400,'message'=>'该选项已存在，不能重复添加！');
                }

                $sys_item_multilevel->item_id = generate_seqno('seqno_item_multilevel');
                $sys_item_multilevel->creator = $arg_array['creator'];
                //$sys_item_multilevel->item_name = $arg_array['m_item_name'];
                //$sys_item_multilevel->parent_id = $arg_array['m_parent_id'];
                $sys_item_multilevel->sort_order = $arg_array['sort_order'];
                //$sys_item_multilevel->region_level = 2;
                $this->dbca->save($sys_item_multilevel);


                $search_son = $this->dbca->stmt_query('SELECT count(*) as num FROM sys_item_multilevel WHERE parent_id = ?','i',$sys_item_multilevel->parent_id);

                if($search_son[0]->num == 1){
                    $hasMid = 1;
                }else if($search_son[0]->num > 1){
                    $hasMid = 2;
                }else{
                    $hasMid = 0;
                }

                $return_array['item_id'] = $sys_item_multilevel->item_id;
                $return_array['item_name'] = $sys_item_multilevel->item_name;
                $return_array['sort_order'] = $sys_item_multilevel->sort_order;
                $return_array['parent_id'] = $sys_item_multilevel->parent_id;
                $return_array['hasMid'] = $hasMid;

                return array('code'=>200,'message'=>'保存成功', 'data'=>$return_array);
            }



        }catch(Exception $e){

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 保存 编辑第三级信息
     * @param $arg_array
     * @return array|null
     */
    public function saveThirdLevel(&$arg_array){
        if(!isset($arg_array['item_name'], $arg_array['sort_order'])){
            return array('code'=>400,'message'=>'参数错误');
        }

        try{

            $sys_item_multilevel = new entity('sys_item_multilevel');

            if(isset($arg_array['aid']) && !empty($arg_array['aid'])){
                $sys_item_multilevel->item_id = $arg_array['editStr'];
                if(!$this->dbca->fetch($sys_item_multilevel)){
                    return array('code'=>400,'message'=>'该选项不存在');
                }

                $sql = 'SELECT COUNT(*) AS num ,item_id FROM sys_item_multilevel WHERE level = 3 AND item_name = "'.$arg_array['item_name'] .'" AND parent_id='.$sys_item_multilevel->parent_id;

                $count_arr = &$this->dbca->stmt_query($sql);

                if( $count_arr[0]->num  > 0 && $count_arr[0]->item_id !=$arg_array['editStr'] ){
                    return array('code'=>400,'message'=>'该选项已存在，请重新编辑');
                }

                $sys_item_multilevel->item_name = $arg_array['item_name'];
//                $sys_item_multilevel->parent_id = $arg_array['pid'];
                $sys_item_multilevel->sort_order = $arg_array['sort_order'];
                $sys_item_multilevel->level = 3;
                $this->dbca->save($sys_item_multilevel);

                $insert_id = $this->obtainSortNum('sys_item_multilevel');

                return array('code'=>200,'message'=>'保存成功','insert_id'=>$insert_id);
            }else{

                $sys_item_multilevel = new entity('sys_item_multilevel');
                $sys_item_multilevel->item_name = $arg_array['item_name'];
                $sys_item_multilevel->level = 3;
                $sys_item_multilevel->parent_id = $arg_array['editStr'];
                if($this->dbca->fetch($sys_item_multilevel)){
                    return array('code'=>400,'message'=>'该选项已存在，不能重复添加！');
                }

                $sys_item_multilevel->item_id = generate_seqno('seqno_item_multilevel');
                $sys_item_multilevel->creator = $arg_array['creator'];
                //$sys_item_multilevel->item_name = $arg_array['s_item_name'];
                //$sys_item_multilevel->parent_id = $arg_array['s_item_id'];
                $sys_item_multilevel->sort_order = $arg_array['sort_order'];
                //$sys_item_multilevel->level = 3;
                $this->dbca->save($sys_item_multilevel);

                $search_son = $this->dbca->stmt_query('SELECT count(*) as num FROM sys_item_multilevel WHERE parent_id = ?','i',$sys_item_multilevel->parent_id);

                if($search_son[0]->num == 1){
                    $hasMid = 1;
                }else if($search_son[0]->num > 1){
                    $hasMid = 2;
                }else{
                    $hasMid = 0;
                }

                $return_array['item_id'] = $sys_item_multilevel->item_id;
                $return_array['item_name'] = $sys_item_multilevel->item_name;
                $return_array['sort_order'] = $sys_item_multilevel->sort_order;
                $return_array['parent_id'] = $sys_item_multilevel->parent_id;
                $return_array['hasMid'] = $hasMid;

                return array('code'=>200,'message'=>'保存成功', 'data'=>$return_array);
            }



        }catch(Exception $e){
            error_log($e->getMessage());
            return null;
        }


    }


    /**
     * 保存第四级信息
     * @param $arg_array
     * @return array|null
     */
    public function saveFourthLevel(&$arg_array){

        if(!isset($arg_array['item_name'], $arg_array['sort_order'])){
            return array('code'=>400,'message'=>'参数错误');
        }

        try{

            $sys_item_multilevel = new entity('sys_item_multilevel');

            if(isset($arg_array['aid']) && !empty($arg_array['aid'])){
                $sys_item_multilevel->item_id = $arg_array['editStr'];
                if(!$this->dbca->fetch($sys_item_multilevel)){
                    return array('code'=>400,'message'=>'该选项不存在');
                }

                $sql = 'SELECT COUNT(*) AS num ,item_id FROM sys_item_multilevel WHERE level = 4 AND item_name = "'.$arg_array['item_name'] .'" AND parent_id='.$sys_item_multilevel->parent_id;

                $count_arr = &$this->dbca->stmt_query($sql);

                if( $count_arr[0]->num  > 0 && $count_arr[0]->item_id !=$arg_array['editStr'] ){
                    return array('code'=>400,'message'=>'该选项已存在，请重新编辑');
                }

                $sys_item_multilevel->item_name = $arg_array['item_name'];
//                $sys_item_multilevel->parent_id = $arg_array['pid'];
                $sys_item_multilevel->sort_order = $arg_array['sort_order'];
                $sys_item_multilevel->level = 4;
                $this->dbca->save($sys_item_multilevel);

                $insert_id = $this->obtainSortNum('sys_item_multilevel');

                return array('code'=>200,'message'=>'保存成功','insert_id'=>$insert_id);
            }else{

                $sys_item_multilevel = new entity('sys_item_multilevel');
                $sys_item_multilevel->item_name = $arg_array['item_name'];
                $sys_item_multilevel->level = 4;
                $sys_item_multilevel->parent_id = $arg_array['editStr'];
                if($this->dbca->fetch($sys_item_multilevel)){
                    return array('code'=>400,'message'=>'该选项已存在，不能重复添加！');
                }

                $sys_item_multilevel->item_id = generate_seqno('seqno_item_multilevel');
                $sys_item_multilevel->creator = $arg_array['creator'];
                //$sys_item_multilevel->item_name = $arg_array['item_name'];
                $sys_item_multilevel->sort_order = $arg_array['sort_order'];
                //$sys_item_multilevel->level = 4;
                $this->dbca->save($sys_item_multilevel);

                $search_son = $this->dbca->stmt_query('SELECT count(*) as num FROM sys_item_multilevel WHERE parent_id = ?','i',$sys_item_multilevel->parent_id);

                if($search_son[0]->num == 1){
                    $hasMid = 1;
                }else if($search_son[0]->num > 1){
                    $hasMid = 2;
                }else{
                    $hasMid = 0;
                }

                $return_array['item_id'] = $sys_item_multilevel->item_id;
                $return_array['item_name'] = $sys_item_multilevel->item_name;
                $return_array['sort_order'] = $sys_item_multilevel->sort_order;
                $return_array['parent_id'] = $sys_item_multilevel->parent_id;
                $return_array['hasMid'] = $hasMid;

                return array('code'=>200,'message'=>'保存成功', 'data'=>$return_array);
            }



        }catch(Exception $e){
            error_log($e->getMessage());
            return null;
        }

    }

    /**
     * 删除选项信息
     * @param $arg_array
     * @return array|null
     */
    public function deleteItemMultilevel(&$arg_array){

        if(!isset($arg_array['delete_str']) || !isset($arg_array['markType'])){
            return array('code'=>400,'message'=>'参数错误');
        }

        try{

            $b_arr = array();//删除数组ID
            //markType 为删除第一级 第二级 第三级 第四级 类型
            switch($arg_array['markType']){
                case 'b':
                    $b_arr[] = $arg_array['delete_str'];

                    //删除第一级
                    $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$arg_array['delete_str']);

                    $m_region = $this->dbca->stmt_query('SELECT item_id FROM sys_item_multilevel where parent_id = ?','i',$arg_array['delete_str']);

                    if($m_region){

                        foreach($m_region as $val){
                            $b_arr[] = $val->item_id;
                            //删除第二级
                            $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$val->item_id);

                            $s_region = $this->dbca->stmt_query('SELECT item_id FROM sys_item_multilevel where parent_id = ?','i',$val->item_id);

                            if($s_region){
                                foreach($s_region as $val1){
                                    $b_arr[] = $val1->item_id;
                                    //删除第三级
                                    $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$val1->item_id);

                                    $l_region = $this->dbca->stmt_query('SELECT item_id FROM sys_item_multilevel where parent_id = ?','i',$val1->item_id);

                                    if($l_region){
                                        foreach($l_region as $val2){
                                            $b_arr[] = $val2->item_id;
                                            //删除第四级
                                            $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$val2->item_id);
                                        }
                                    }
                                }
                            }

                        }

                    }

                    break;

                case 'm':

                    $b_arr[] = $arg_array['delete_str'];

                    //删除第二级
                    $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$arg_array['delete_str']);

                    $s_region = $this->dbca->stmt_query('SELECT item_id FROM sys_item_multilevel where parent_id = ?','i',$arg_array['delete_str']);

                    if($s_region){
                        foreach($s_region as $val1){
                            $b_arr[] = $val1->item_id;
                            //删除第三级
                            $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$val1->item_id);

                            $l_region = $this->dbca->stmt_query('SELECT item_id FROM sys_item_multilevel where parent_id = ?','i',$val1->item_id);

                            if($l_region){
                                foreach($l_region as $val2){
                                    $b_arr[] = $val2->item_id;
                                    //删除第四级
                                    $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$val2->item_id);
                                }
                            }
                        }
                    }

                    break;

                case 's':
                    $b_arr[] = $arg_array['delete_str'];

                    //删除第三级
                    $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$arg_array['delete_str']);

                    $s_region = $this->dbca->stmt_query('SELECT item_id FROM sys_item_multilevel where parent_id = ?','i',$arg_array['delete_str']);

                    if($s_region){
                        foreach($s_region as $val1){
                            //删除第四级
                            $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$val1->item_id);
                            $b_arr[] = $val1->item_id;
                        }
                    }

                    break;

                case 'l':
                    $b_arr[] = $arg_array['delete_str'];
                    //删除第四级
                    $this->dbca->stmt_query('DELETE FROM sys_item_multilevel WHERE item_id = ?','i',$arg_array['delete_str']);
                    break;

            }

            return array('code'=>200,'message'=>'删除成功','midArr'=>$b_arr);


        }catch(Exception $e){

            error_log($e->getMessage());
            return null;

        }

    }


    public function obtainSortNum($table_name, $sort_field = 'sort_order') {

        global $G_SHOP;

        if (!isset($table_name) || empty($table_name)) {
            return 0;
        }

        try {

            //查询最大排序序号
            $search_sql = sprintf('SELECT IF(MAX(%s) IS NULL ,1 , MAX(%s)+1) AS max_sort FROM %s', $sort_field, $sort_field, $table_name);
            $sort_array = $G_SHOP->DBCA->stmt_query($search_sql);

            //返回排序序号
            return $sort_array[0]->max_sort;
        } catch (Exception $e) {
            error_log($e->getMessage());
            return 0;
        }
    }


}